const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cookie:[],
    mate:null,
    https:"https://csb2c.fenxiangqu.cn/",
    newIndex:1,
    spsl:0,
    commnum:1,
    id:null,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self  = this;
    var a = wx.getStorageSync('cookie') || [];
    this.setData({
      cookie: a
    })
      wx.request({
        url: "https://csb2c.fenxiangqu.cn/xcx_wx_commodityInt",
        data: {
          id: options.id
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': "LID=" + self.data.cookie
        },
        method: "POST",
        success: res => {
          console.log(res);
          res.data[0].spscj = Number(res.data[0].spscj).toFixed(2);
          self.setData({
            mate:res.data,
            spsl:res.data[0].spkc
          })
        },
        fail: res => {
          wx.showToast({
            title: '网络故障！',
            icon: 'success',
            duration: 1000
          })
        }
      })
  },
  startOne(e){
    const index = e.currentTarget.dataset.index;
    this.setData({
      newIndex: index

    })

  },
  gwc(e){
    var self = this;
  },
  addCom(e){
   const self = this;
   var num = self.data.commnum;
       num++;
       if(num>=999){
         self.setData({
           commnum:999
         })
       } else if (num >= self.data.spsl){
         if (self.data.spsl == 0) {
           wx.showToast({
             title: '抱歉,商品库存不足!',
             icon: 'success',
             duration: 1000
           })
         } else {
           wx.showToast({
             title: '抱歉,商品库存只有' + self.data.spsl+'件!',
             icon: 'success',
             duration: 1000
           })
         }
       }else{
         self.setData({
           commnum: num
         })
       }
  },
  cutCom:function(e){
    const self = this;
    var num = self.data.commnum;
    if (num > 1) {
      num--;
      self.setData({
        commnum: num
      })
    }
    else {
      wx.showToast({
        title: '数量不能小于等于0!',
        icon: 'success',
        duration: 1000
      })
    }
  },
  gwc:function(e){
    const self = this;
    const id = e.currentTarget.dataset.id,
          num = e.currentTarget.dataset.num;
          if(parseFloat(num)>0){
            wx.request({
              url: "https://csb2c.fenxiangqu.cn/xcx_addgwc_wx",
              data: {
                xl: num,
                id:id
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Cookie': "LID=" + self.data.cookie
              },
              method: "POST",
              success: res => {
                if(res.data=="login"){
                  wx.showModal({
                    title: '登录提示',
                    content: '您未登录，是否前往登录？',
                    success: function (res) {
                      if (res.confirm) {
                        wx.switchTab({
                          url: '../center/center'
                        })
                      } else if (res.cancel) {
                        wx.switchTab({
                          url: '../index/index'
                        })
                      }
                    }
                  })
                }else{
                  if(res.data.error != undefined){
                    wx.showToast({
                      title: '网络故障！',
                      icon: 'success',
                      duration: 1000
                    })
                  }else{
                    wx.showToast({
                      title: '已成功加入购物车！',
                      icon: 'success',
                      duration: 1000
                    })
                    wx.request({
                      url: "https://csb2c.fenxiangqu.cn/xcx_50010_wx",
                      data: {
                        a:""
                      },
                      header: {
                        'content-type': 'application/x-www-form-urlencoded',
                        'Cookie': "LID=" + self.data.cookie
                      },
                      method: "POST",
                      success: res => {
                       console.log(res)
                      },
                      fail: res => {
                        wx.showToast({
                          title: '网络故障！',
                          icon: 'success',
                          duration: 1000
                        })
                      }

                    })
                  }
                }
              },
              fail: res => {
                wx.showToast({
                  title: '网络故障！',
                  icon: 'success',
                  duration: 1000
                })
              }

            })
          }else{
            wx.showToast({
              title: '请输入大于0数值!',
              icon: 'success',
              duration: 1000
            })
          }
          
  },
  buyNow (res){
    const id = res.currentTarget.dataset.id,self = this;
        app.globalData['yyyyy']=""
        wx.redirectTo({
          url: '../submit/submit?spidlist=' + id + '&type=comm' + '&num=' + self.data.commnum + '&gmtype=ljgm'
        })
  },
  indexf(){
    wx.switchTab({
      url: '../home/home'
    })
  },
  goo(){
    wx.switchTab({
      url: '../goods/goods',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})